﻿using System;
using System.Collections.Generic;
using NHibernate;
using System.Text;

namespace Ecreate.Popedom.Service.SpringImpl
{
    public class UserServiceImpl : IUserService
    {
        private Service.IUserIdService IdService = new Service.SpringImpl.UserIdServiceImpl();

        private Dao.Hibernate.UserDaoImpl Dao = new Ecreate.Popedom.Dao.Hibernate.UserDaoImpl();
        private Dao.Hibernate.GroupDaoImpl GroupDao = new Ecreate.Popedom.Dao.Hibernate.GroupDaoImpl();
        public IList<tSysuser> getAll()
        {
            return Dao.getAll();
        }
        public tSysuser getById(string id)
        {

            return Dao.getById(id);
        }


        public IList<tSysuser> getUsersByGroupID(long id)
        {
            tGroup t=GroupDao.getByID(id);
            return t.Users;
        }

        public void InsertUser(tSysuser model)
        {

            Dao.InsertUser(model);
        }
        public void DeleteUser(tSysuser model)
        {
            Dao.DeleteUser(model);
        }
        public void UpdateUser(tSysuser model)
        {

            Dao.UpdateUser(model);
        }

        public Web.WebResult<tSysuser> UpdatePassword(tSysuser systemuser)
        {

            try
            {
                Web.WebResult<tSysuser> result = new Web.WebResult<tSysuser>();
                tSysuser searchUser = Dao.getById(systemuser.Id);
                if (searchUser == null)
                {
                    result.Message = "用户不存在!";
                    result.Success = false;
                }
                else
                {
                    {

                        Dao.UpdateUser(searchUser);
                        result.Success = true;
                        result.Message = " 更新密码成功!";
                    }
                }
                return result;
            }
            catch (Exception ex)
            {
                Web.WebResult<tSysuser> result = new Web.WebResult<tSysuser>();
                result.Success = false;
                result.Message = ex.Message;
                result.Message = result.Message.Replace("'", "");
                result.Message = result.Message.Replace("\"", "");
                result.Message = result.Message.Replace("/", "");
                result.Message = result.Message.Replace("\\", "");
                result.Message = result.Message.Replace(">", "");
                result.Message = result.Message.Replace("<", "");
                result.Message = result.Message.Replace("-", "");
                return result;
            }

        }


        public Web.WebResult<tSysuser> CheckUser(tSysuser systemuser)
        {
            try
            {
                Web.WebResult<tSysuser> result = new Web.WebResult<tSysuser>();
                tSysuser searchUser=Dao.getById(systemuser.Id);
                if (searchUser== null)
                {
                    result.Message = "用户不存在!";
                    result.Success = false;
                }
                else
                {
                    if (searchUser.Password.Equals(systemuser.Password))
                    {
                        result.Success = true;
                        result.Message = "sucess";
                        result.ResultObject = searchUser;
                    }
                    else
                    {
                        result.Success = false;
                        result.Message = "密码错误!";
                    }
                }
                return result;
            }
            catch (Exception ex)
            {
                Web.WebResult<tSysuser> result = new Web.WebResult<tSysuser>();
                result.Success = false;
                result.Message = ex.Message;
                result.Message = result.Message.Replace("'", "");
                result.Message = result.Message.Replace("\"", "");
                result.Message = result.Message.Replace("/", "");
                result.Message = result.Message.Replace("\\", "");
                result.Message = result.Message.Replace(">", "");
                result.Message = result.Message.Replace("<", "");
                result.Message = result.Message.Replace("-", "");
                return result;
            }
        }


        private static string SeprateSy = "-";
        public string getByZeroId(tSysuser model)
        {
            Ecreate.Popedom.tLevelzeroUserid id = new Ecreate.Popedom.tLevelzeroUserid();
            IdService.InsertId(id);
            return "admin" + SeprateSy + id.Id.ToString();
        }
        public string getByOneId(tSysuser model)
        {
            Ecreate.Popedom.tLeveloneUserid id = new Ecreate.Popedom.tLeveloneUserid();
            IdService.InsertId(id);

            string provincecode = model.Province.Id.ToString();
            provincecode = provincecode.PadRight(6, '0');

            return provincecode + SeprateSy + id.Id.ToString();
        }
        public string getByTwoId(tSysuser model)
        {
            Ecreate.Popedom.tLeveltwoUserid id = new Ecreate.Popedom.tLeveltwoUserid();
            IdService.InsertId(id);

            string citycode = model.City.Id.ToString();
            citycode = citycode.PadRight(6, '0');

            return citycode + SeprateSy + id.Id.ToString();
        }
        public string getByThreeId(tSysuser model)
        {
            Ecreate.Popedom.tLevelthreeUserid id = new Ecreate.Popedom.tLevelthreeUserid();
            IdService.InsertId(id);
            return "xcy" + SeprateSy + id.Id.ToString();
        }


        public IList<tSysuser> getAllZero()
        {
            return Dao.getAllZero();

        }
        public IList<tSysuser> getAllOne()
        {
            return Dao.getAllOne();

        }
        public IList<tSysuser> getAllTwo()
        {
            return Dao.getAllTwo();
        }
        public IList<tSysuser> getAllThree()
        {
            return Dao.getAllThree();
        }

    }
}
